/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.params.service.impl;

import me.zhengjie.modules.app.params.domain.ParamsInfo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.app.params.repository.ParamsInfoRepository;
import me.zhengjie.modules.app.params.service.ParamsInfoService;
import me.zhengjie.modules.app.params.service.dto.ParamsInfoDto;
import me.zhengjie.modules.app.params.service.dto.ParamsInfoQueryCriteria;
import me.zhengjie.modules.app.params.service.mapstruct.ParamsInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Haojie Li
* @date 2021-03-06
**/
@Service
@RequiredArgsConstructor
public class ParamsInfoServiceImpl implements ParamsInfoService {

    private final ParamsInfoRepository paramsInfoRepository;
    private final ParamsInfoMapper paramsInfoMapper;

    @Override
    public Map<String,Object> queryAll(ParamsInfoQueryCriteria criteria, Pageable pageable){
        Page<ParamsInfo> page = paramsInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(paramsInfoMapper::toDto));
    }

    @Override
    public List<ParamsInfoDto> queryAll(ParamsInfoQueryCriteria criteria){
        return paramsInfoMapper.toDto(paramsInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ParamsInfoDto findById(String id) {
        ParamsInfo paramsInfo = paramsInfoRepository.findById(id).orElseGet(ParamsInfo::new);
        ValidationUtil.isNull(paramsInfo.getId(),"ParamsInfo","id",id);
        return paramsInfoMapper.toDto(paramsInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ParamsInfoDto create(ParamsInfo resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return paramsInfoMapper.toDto(paramsInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ParamsInfo resources) {
        ParamsInfo paramsInfo = paramsInfoRepository.findById(resources.getId()).orElseGet(ParamsInfo::new);
        ValidationUtil.isNull( paramsInfo.getId(),"ParamsInfo","id",resources.getId());
        paramsInfo.copy(resources);
        paramsInfoRepository.save(paramsInfo);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            paramsInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ParamsInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ParamsInfoDto paramsInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("参数名称", paramsInfo.getParamName());
//            map.put("参数标识", paramsInfo.getParamTag());
//            map.put("参数值", paramsInfo.getParamValue());
            map.put(" createBy",  paramsInfo.getCreateBy());
            map.put(" updateBy",  paramsInfo.getUpdateBy());
            map.put(" createTime",  paramsInfo.getCreateTime());
            map.put(" updateTime",  paramsInfo.getUpdateTime());
            map.put("数据状态", paramsInfo.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

//	@Override
//	public ParamsInfo findByParamTag(String paramTag) {
//		// TODO Auto-generated method stub
//		List<ParamsInfo> tempList = paramsInfoRepository.findByParamTag(paramTag);
//		if(tempList != null && tempList.size() > 0) {
//			return tempList.get(0);
//		}
//		return null;
//	}
}